import requests


def get_comments(url):
    # 传给它referer
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) "
                      "Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.52",
        "referer": "https://music.163.com/"
    }
    params = "QAMeUi4Q5T7wD48CYC8X6HRLhmbT5GWf4cYFf1etQTC7KRkZBQ7/J+ijo3lM4n1fN/uvrSLj+Ln3Auc8FEIBSDn25ah8610vbh" \
             "/ub0Egb0YhiAlTNZ9AyVJJ8G4kFKwq9v9R8AM70KU7ud1uGid43oyH6qgAa0BYLh4dvNKciFI4qkzLU4xzevBT3LxNsx2YyaNgmr9" \
             "d0W0I7InsPgB99e8BYL6SsUETwdAzTADcFVfIqQjx3qnDiwCq9WPLMMXdpQtPtZJLti3OaBEQokLq5Q== "
    encSecKey = "3ccfcc2653ce791c8645a98bfd5a579b660b3f82ffb94ed4ed0e792feb8c105cb0fb2ee1108791c6c5e74f6ce19b7bdfd69" \
                "d588ec585271b64c9d1f4ef5939fd179c2c8310509385224da397e025f8ef3b5e6edbc554863b16ded65fbf169fa7ebd100172" \
                "a8e7bb5a78eca6eb6181270b970ea67d3f13e9861a4caf682b0044f"
    data = {
        "params": params,
        "encSecKey": encSecKey
    }
    name_id = url.split("=")[1]
    target_url = "http://music.163.com/weapi/comment/resource/comments/get?csrf_token=".format(name_id)
    res = requests.post(target_url, headers=headers, data=data)
    return res


def main():
    url = input("请输入链接地址：")
    res = get_comments(url)
    with open("data.txt", "w", encoding="utf-8") as file:
        file.write(res.text)


if __name__ == "__main__":
    main()
